package com.hzpd.hainan.module.usercenter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzpd.hainan.R;
import com.hzpd.hainan.app.BaseFragment;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.module.usercenter.acticity.AccountSetActivity;
import com.hzpd.hainan.module.usercenter.acticity.CollectionActivity;
import com.hzpd.hainan.module.usercenter.acticity.CommActivity;
import com.hzpd.hainan.module.usercenter.acticity.LoginActivity;
import com.hzpd.hainan.module.usercenter.acticity.SystemSetActivity;
import com.hzpd.hainan.utils.AAnim;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.TUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by 猪猪侠 on 2017/7/3.
 */

public class UserCenterFragment extends BaseFragment {
    @BindView(R.id.photo_usercenter_main)
    ImageView photo_usercenter_main;
    @BindView(R.id.name_usercenter_main)
    TextView name_usercenter_main;



    LoginQuitBR br;
    public static final String ACTION_USER = "com.hzpd.cms.user";
    public static final String ACTION_QUIT = "com.hzpd.cms.quit";//退出登录;
    public static final String ACTION_QUIT_LOGIN = "com.hzpd.cms.quit.login";
    @BindView(R.id.wodepinglung_id)
    TextView wodepinglungId;
    @BindView(R.id.wodeshoucang_id)
    TextView wodeshoucangId;
    @BindView(R.id.zhanghushezhi_id)
    TextView zhanghushezhiId;
    @BindView(R.id.xitongshezhi_id)
    TextView xitongshezhiId;
    Unbinder unbinder;
    @Override
    public int setMyContentView() {
        return R.layout.fragment_usercenter_main;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        br = new LoginQuitBR();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_QUIT);
        filter.addAction(ACTION_USER);
        filter.addAction(ACTION_QUIT_LOGIN);
        activity.registerReceiver(br, filter);
        if (spu.getUser() != null) {
            Glide.with(getActivity()).load(spu.getUser().getAvatar_path()).placeholder(R.drawable.pic_touxiang).dontAnimate().into(photo_usercenter_main);
            name_usercenter_main.setText(spu.getUser().getNickname());
        }

    }

    public static UserCenterFragment newInstance() {
        UserCenterFragment userCenterFragment = new UserCenterFragment();
        return userCenterFragment;
        /////////
    }

    @Override
    public void initView() {

        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            wodepinglungId.setText(getResources().getString(R.string.wodepingling));
            wodeshoucangId.setText(getResources().getString(R.string.wodeshoucang));
            zhanghushezhiId.setText(getResources().getString(R.string.zhanghushezhi));
            xitongshezhiId.setText(getResources().getString(R.string.xitongshezhi));
            if(spu.getUser() == null){
                name_usercenter_main.setText("登录");
            }
        }else{
            wodepinglungId.setText(getResources().getString(R.string.wodepingling_zang));
            wodeshoucangId.setText(getResources().getString(R.string.wodeshoucang_zang));
            zhanghushezhiId.setText(getResources().getString(R.string.zhanghushezhi_zang));
            xitongshezhiId.setText(getResources().getString(R.string.xitongshezhi_zang));
            if(spu.getUser() == null){
                name_usercenter_main.setText("ཐོ་འཇུག");
            }
        }
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.user_login_usercenter,
            R.id.comm_usercenter_main,
            R.id.collection_usercenter_main,
            R.id.system_set_usercenter_main,
            R.id.account_set_usercenter_main})
    public void collection(View view) {
        boolean flag = false;
        Intent mIntent = new Intent();
        switch (view.getId()) {
            case R.id.user_login_usercenter:
                if (spu.getUser() == null) {
                    flag = true;
                    mIntent.setClass(activity, LoginActivity.class);
                }
                break;
            case R.id.collection_usercenter_main:
                if (spu.getUser() != null) {
                    flag = true;
                    mIntent.setClass(activity, CollectionActivity.class);
                } else {
                    flag = true;
                    mIntent.setClass(activity, LoginActivity.class);
                    TUtils.toast("请先登录");
                }
                break;
            case R.id.comm_usercenter_main:
                if (spu.getUser() != null) {
                    flag = true;
                    mIntent.setClass(activity, CommActivity.class);
                } else {
                    flag = true;
                    mIntent.setClass(activity, LoginActivity.class);
                    TUtils.toast("请先登录");
                }
                break;
            case R.id.account_set_usercenter_main:
                if (spu.getUser() != null) {
                    flag = true;
                    mIntent.setClass(activity, AccountSetActivity.class);
                } else {
                    flag = true;
                    mIntent.setClass(activity, LoginActivity.class);
                    TUtils.toast("请先登录");
                }
                break;
            case R.id.system_set_usercenter_main:
                flag = true;
                mIntent.setClass(activity, SystemSetActivity.class);
                break;


        }
        if (flag) {
            startActivity(mIntent);
            AAnim.ActivityStartAnimation(activity);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public class LoginQuitBR extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ACTION_USER)) {
                setLogin();
            } else if (action.equals(ACTION_QUIT)) {
                setQuit();
                LogUtils.i("setquit");
            } else if (action.equals(ACTION_QUIT_LOGIN)) {
                setQuit();
                LogUtils.i("setquitlogin");

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        zy_rfrag_ll_login.callOnClick();
                        LogUtils.i("r_login_layout.callOnClick()");
                    }
                }, 500);
            }
        }
    }

    private void setQuit() {
        photo_usercenter_main.setImageResource(R.drawable.pic_touxiang);
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            name_usercenter_main.setText("登录");
        }else{
            name_usercenter_main.setText("ཐོ་འཇུག");
        }


    }

    private void setLogin() {
        Glide.with(getActivity()).load(spu.getUser().getAvatar_path()).placeholder(R.drawable.pic_touxiang).dontAnimate().into(photo_usercenter_main);
        name_usercenter_main.setText(spu.getUser().getNickname());
    }
}
